﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ContactService
/// </summary>
/// 
namespace FacebookApp
{
    public class ContactService
    {
        #region[Contact_GetbyAll]
        public static List<Contact> Contact_GetbyAll()
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    var da = (from ct in dt.Contacts select ct).ToList();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }

        }
        #endregion

        #region[Contact_GetbyId]
        public static Contact Contact_GetbyId(string Data)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    var da = (from ct in dt.Contacts where ct.Id == id select ct).First();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion

        #region[Contact_GetbyTop]
        public static List<Contact> Contact_GetbyTop(string Top, string Where, string Order)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    string query = "Select * from [Contact]";
                    if (!Top.Equals(""))
                        query = " Select top " + Top + " * from [Contact] ";
                    if (!Where.Equals(""))
                        query += " where " + Where + " ";
                    if (!Order.Equals(""))
                        query += " order by " + Order;

                    IEnumerable<Contact> results = dt.ExecuteQuery<Contact>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }
        #endregion

        #region[Contact_Insert]
        public static void Contact_Insert(ContactInfo Data)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    Contact new_contact = new Contact();
                    new_contact.Name = Data.Name;
                    new_contact.Company = Data.Company;
                    new_contact.Address = Data.Address;
                    new_contact.Phone = Data.Phone;
                    new_contact.Mail = Data.Mail;
                    new_contact.Detail = Data.Detail;
                    new_contact.Date = DateTime.Now;
                    new_contact.Status = bool.Parse(Data.Status);
                    dt.Contacts.InsertOnSubmit(new_contact);
                    dt.SubmitChanges();
                }
            }
            catch (Exception e)
            {
                WebMsgBox.Show(Data.Status);
            }

        }
        #endregion

        #region[Contact_Update]
        public static void Contact_Update(ContactInfo Data)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data.Id);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    Contact da = dt.Contacts.Single(ct => ct.Id == id);
                    da.Name = Data.Name;
                    da.Company = Data.Company;
                    da.Address = Data.Address;
                    da.Phone = Data.Phone;
                    da.Mail = Data.Mail;
                    da.Detail = Data.Detail;
                    //da.Date = DateTime.Now;
                    da.Status = bool.Parse(Data.Status);
                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
        }
        #endregion

        #region[Contact_Delete]

        public static void Contact_Delete(string Data)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    Contact da = dt.Contacts.Single(ct => ct.Id == id);
                    dt.Contacts.DeleteOnSubmit(da);

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
        }
        #endregion
    }
    public class ContactInfo
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Company { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
        public string Mail { get; set; }
        public string Detail { get; set; }
        public string Date { get; set; }
        public string Status { get; set; }
    }
}